
<script type="text/javascript">
  /*
  * 公共文件 存放公共数据 以及方法
  **/
  //服务器地址
    const apiUrl = "http://192.168.1.12:8082"; //本地测试端口
//    const apiUrl = 'http://www.yingla9.com/yl'; //服务器测试端口
  const sourceUrl = 'http://www.yingla9.com/source/'; //服务器测试端口


  const libg = ["#FCBD06", "#FB3C3D", "#50BDEF", "#FF9452", "#FF6978", "#E65BEB", "#72A4FF", "#47D844", "#3BD3F2", "#FF6A4D"];//标签背景颜色
  const mapKey='c7af13d7326531bf138c8580d96a7ea1';
  const province=[
    "北京","天津","河北","山西","内蒙古","辽宁","吉林","黑龙江","上海","江苏",
    "浙江","安徽","福建","江西","山东","河南","湖北","湖南","广东","广西",
    "海南","重庆","四川","贵州","云南","西藏","陕西","甘肃省","青海","宁夏",
    "新疆","台湾","香港特别行政区","澳门"];
  //获取cookie、
  function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
      return (arr[2]);
    else
      return null;
  }

  //设置cookie,增加到vue实例方便全局调用
  function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
  }

  //删除cookie
  function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
      document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
  }
  //页面分享
  function wxShare(url, title,text,obj) {
    //var uid=window.localStorage.getItem("uid");
    var name=window.localStorage.getItem("name");
    if(name==""||name=="undefined"||name==undefined){
      name=window.localStorage.getItem("username")
    }
//    var text="";
//    if(url.indexOf("?")>0){
//      text="&uid="+uid;
//    }else{
//      text="?uid="+uid;
//    }
    console.log(url);
    console.log(text);

    if(text.indexOf("invitation")>0){
      name=name+"向您发出邀请";
    }else{
      name=name+"向您推荐英拉助手";
    }
    obj.$http.get(apiUrl+"/wx/jssign?url=" + url).then(res => {
      if (res.data.code=="0") {
        var sign=res.data.data;
        wx.config({
          debug: false,
          appId: 'wxb2e099eb45c56187',
          timestamp: sign.timestamp,
          nonceStr: sign.nonceStr,
          signature: sign.signature,
          jsApiList: [
            'onMenuShareTimeline', 'onMenuShareAppMessage'// 所有要调用的 API 都要加到这个列表中
          ]
        });
        wx.ready(function () {
          wx.checkJsApi({
            jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
            success: function (res) {

              // 以键值对的形式返回，可用的api值true，不可用为false
              // 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
            }
          });
          // 在这里调用 API
          wx.onMenuShareTimeline({  //朋友圈
            title: name, // 朋友圈分享描述、标题
            link: text, // 分享链接
            imgUrl: 'http://www.yingla9.com/source/yl/logo.jpg', // 分享图标
            success: function () {
              // 用户确认分享后执行的回调函数
            },
            cancel: function () {
              // 用户取消分享后执行的回调函数
            }
          });
          wx.onMenuShareAppMessage({
            title: title, // 对话分享标题
            desc: name, // 分享描述
            link: text, // 分享链接
            imgUrl: 'http://www.yingla9.com/source/yl/logo.jpg', // 分享图标
            type: '', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () {
              // 用户确认分享后执行的回调函数
            },
            cancel: function () {
              // 用户取消分享后执行的回调函数
            }
          });
        });
      }
    })



  }
  //绑定关系
  function bindRelation(uid,obj) {
    if(uid){
      let params = new URLSearchParams();
      params.append("uid", uid);
      let url = apiUrl;
      obj.$http({
        url: url + '/user/bindRelation',
        method: 'post',
        data: params,
      }).then(res => {
        window.localStorage.setItem("shareId",0)

      }).catch(function (err) {
        window.localStorage.setItem("shareId",0)
        console.error(err);
      });
    }
  }
  export default {
    apiUrl, libg,
    setCookie: setCookie,
    getCookie: getCookie,
    delCookie: delCookie,
    wxShare:wxShare,
    sourceUrl: sourceUrl,
    mapKey:mapKey,
    province:province,
    bindRelation:bindRelation,
  }
  //  const apiUrl="http://yl.xmluc.com:8080/yl"; //线上端口
</script>

